package com.moko;

import com.moko.common.AsyncImageLoader;
import com.moko.common.AsyncImageLoaderCallback;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private AsyncImageLoader loader = AsyncImageLoader.getInstance();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		loadImage("http://img1.moko.hk/users/0/13/4181/post/68/img1_cover_4625275.jpg", R.id.imageView01);
		loadImage("http://img1.moko.hk/users/0/2/835/post/60/img1_cover_4617723.jpg", R.id.imageView02);
		loadImage("http://img1.moko.hk/pics/guanggao/c2/img1_src_4622492.jpg", R.id.imageView03);
	}

	// url：下载图片的URL id：ImageView控件的ID
	private void loadImage(final String url, final int id) {
		// 如果缓存过就会从缓存中取出图像，ImageCallback接口中方法也不会被执行
		ImageView imageView = (ImageView) findViewById(id);
		AsyncImageLoaderCallback callbackImpl = new AsyncImageLoaderCallback(imageView);
		Bitmap cacheImage = loader.loadBitmap(url, callbackImpl);
		if (cacheImage != null) {
			imageView.setImageBitmap(cacheImage);
		}
	}
}